package tutorials;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.border.LineBorder;


/**
 *
 * @author timon
 */
public class CustomStyleList {
  
  /**
   * A cell renderer, which takes list items (as objects) and
   * translates them into list selection displays.  Used 
   * automatically by JList
   */
  static class CustomCellRenderer
    implements ListCellRenderer {

    @Override
    /**
     * When the CustomCellRenderer is loaded into our JList,
     * the list will call this method automatically to get the 
     * Components which are used for selection on the list.
     */
    public Component getListCellRendererComponent(
      JList list, // The list which is calling this method
      Object value, // The object which represents the value
      int index, // The index # of this object within the list
      boolean isSelected, // Indicates that this item is currently
                          // selected.
      boolean cellHasFocus // Indicates whether the cell currently 
                           // has the focus
      ) {
      
      // Cast our 'value' object into an object of our known class
      CustomListItem item = (CustomListItem) value;
      
      // Create a new JLabel (we'll return this to the list)
      JLabel cell = new JLabel();
      
      // Set the preferred size for each display 'label'
      cell.setPreferredSize(new Dimension(100,20));
      
      // Set the text portion of the cell, according to the value
      // object's .toString() method.
      cell.setText(item.getName());
      
      // Set the image portion of the cell, acquiring it from the 
      // value object.
      cell.setIcon(item.getIcon());
      
      // Do dome styling to the label.
      cell.setIconTextGap(10); // places 10 px between image & text
      cell.setFont(new Font( 
              "Times New Roman", 
              Font.PLAIN,
              12)); // Set ourselves a new font for the cells
      
      // If the current cell is selected
      if (isSelected) {
        // Set a blue border and backdrop
        cell.setBorder(new LineBorder(Color.blue));
        cell.setBackground(new Color(220,220,235));
      }
      else
      {
        // Set the deault background
        cell.setBorder(new LineBorder(list.getBackground()));
        cell.setBackground(list.getBackground());
      }
      
      // Turn opacity on so you can see our background changes
      cell.setOpaque(true);
      
      // Return the cell to the list for display
      return cell;
    }
  }
  
  public static void main(String[] args) {
    
    // Create 9 custom list items 
    // (there are more effective ways to set up items, this is 
    // drawn out so you can get an idea of exactly what goes into
    // making our CustomListItem objects, and how exactly the JList
    // can be loaded up via constructor.
    CustomListItem item1 = new CustomListItem();
    CustomListItem item2 = new CustomListItem();
    CustomListItem item3 = new CustomListItem();
    CustomListItem item4 = new CustomListItem();
    CustomListItem item5 = new CustomListItem();
    CustomListItem item6 = new CustomListItem(); 
    CustomListItem item7 = new CustomListItem();
    CustomListItem item8 = new CustomListItem();
    CustomListItem item9 = new CustomListItem();
    item1.setImage(loadImage("./image/tile/DirtFloor.png"));
    item1.setName("Dirt Floor");
    item2.setImage(loadImage("./image/tile/Water.png"));
    item2.setName("Water");
    item3.setImage(loadImage("./image/tile/Grass.png"));
    item3.setName("Grass");
    item4.setImage(loadImage("./image/tile/DirtFloor.png"));
    item4.setName("Dirt Floor 2");
    item5.setImage(loadImage("./image/tile/Water.png"));
    item5.setName("Water 2");
    item6.setImage(loadImage("./image/tile/Grass.png"));
    item6.setName("Grass 2");
    item7.setImage(loadImage("./image/tile/DirtFloor.png"));
    item7.setName("Dirt Floor 3");
    item8.setImage(loadImage("./image/tile/Water.png"));
    item8.setName("Water 3");
    item9.setImage(loadImage("./image/tile/Grass.png"));
    item9.setName("Grass 3");
    
    // Wrap all of the CustomListItem objects into an array
    CustomListItem[] items = {item1, item2, item3, item4, item5,
      item6, item7, item8, item9};
    
    // Create a new list, passing the array into the constructor
    JList list = new JList(items);
    
    // Assign our custom cell renderer to the JList object 
    list.setCellRenderer(new CustomCellRenderer());

    // Place the list inside of a scroll pane, since the JList
    // does not come with a scroll bar.
    JScrollPane scrollPane = new JScrollPane(list);
    
    
    // Setup a frame and place our list within it.
    JFrame frame = new JFrame();
    
    frame.setLayout(new FlowLayout());
    frame.add(scrollPane);
    
    frame.setSize(600,400);
    frame.setTitle("Custom Style List Tutorial");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    
    
  }
  
  public static Image loadImage(String pathname) {
    
    // grab and return image
    try {
      Image image = ImageIO.read(new File(pathname));
      return image;
    } catch (IOException ex) {
      Logger.getLogger(CustomStyleList.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    // if things get messed up
    return null;
    
  }
}
